package com.ebelter.nb.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.nb.R;


public class UserSwitchPopupWindow extends PopupWindow implements View.OnClickListener {
    public View mView;


    public int displayTopIndex;
    public LinearLayout mppw_1_ll;
    public LinearLayout mppw_2_ll;
    public TextView name1_tv;
    public TextView name2_tv;

    private SelectListener mSelectListener;

    public void setmSelectListener(SelectListener mSelectListener) {
        this.mSelectListener = mSelectListener;
    }

    public UserSwitchPopupWindow(Context context) {
        super(context);

        mView = View.inflate(context, R.layout.userswitch_pw, null);

        mppw_1_ll=mView.findViewById(R.id.mppw_1_ll);
        mppw_2_ll=mView.findViewById(R.id.mppw_2_ll);
        name1_tv=mView.findViewById(R.id.mppw_name1_tv);
        name2_tv=mView.findViewById(R.id.mppw_name2_tv);
        mppw_1_ll.setOnClickListener(this);
        mppw_2_ll.setOnClickListener(this);

        setContentView(mView);

        setWidth(DipUtil.getWindowWidth(context));

        this.setFocusable(true);

        this.setOutsideTouchable(true);

        this.update();

        ColorDrawable colorDrawable = new ColorDrawable(0000000000);

        // 点击返回键和其他地方使其消失，设置了这个才能触发OnDismissListener,设置其他控件变化等操作

        this.setBackgroundDrawable(colorDrawable);

//        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.DialogAnimation2);


    }




    public void show(Context context,View anchor,int index) {

        this.displayTopIndex=index;
        if (displayTopIndex==2){
            ViewUtils.setTextViewStr(name1_tv,"用户2");
            ViewUtils.setTextViewStr(name2_tv,"用户1");
        }else {
            ViewUtils.setTextViewStr(name1_tv,"用户1");
            ViewUtils.setTextViewStr(name2_tv,"用户2");
        }


        float ppHeight= context.getResources().getDimension(R.dimen.dp84);
        showAsDropDown(anchor, 0, (int) (-ppHeight/2)-1);
    }

    @Override
    public void onClick(View v) {
        boolean isChange = false;
        int selectIndex = displayTopIndex;
        if (v.getId()==R.id.mppw_1_ll){

        }else if (v.getId()==R.id.mppw_2_ll){
            isChange=true;
            selectIndex=3-selectIndex;
        }
        if (mSelectListener!=null){
            dismiss();
            mSelectListener.select(isChange,selectIndex);}
    }


   public interface SelectListener{
        void select(boolean isChange,int index);
    }
}